﻿using System;

using System.Drawing;
using System.Drawing.Imaging;

using LowLevelGraphics;

namespace LowLevelGraphics.Filter
{
    public delegate Color PixelDelegate(Color sourceColor, int x, int y);

    /// <summary>
    /// This is the normal Lambda delegate
    /// You may use it to create simple filter even at runtime by giving it a string.
    /// Usage: 
    /// </summary>
    public class LambdaCommand : BaseImageFilter
    {
        public PixelDelegate m_PixelDelegate = null;

        // Keep it simple - operate on continuous color formats only
        //protected PixelFormat[] m_SupportedPixelFormats = new PixelFormat[] {
        //    PixelFormat.Format24bppRgb,
        //    PixelFormat.Format24bppRgb,
        //    PixelFormat.Format32bppPArgb
        //};

        /// <summary>
        /// Initializes a new instance of the <see cref="LambdaCommand"/> class.
        /// </summary>
        public LambdaCommand()
            : base()
        {
            // identity command
            m_PixelDelegate = (Color c, int x, int y) => c;
        }

        /// <summary>
        /// DefaultConstructor
        /// </summary>
        /// <param name="pixelDelegate"></param>
        public LambdaCommand(PixelDelegate pixelDelegate)
            : base()
        {
            if (pixelDelegate == null)
                throw new ArgumentNullException("pixelDelegate");
            m_PixelDelegate = pixelDelegate;
        }

        /// <summary>
        /// PixelDelegate
        /// </summary>
        public PixelDelegate PixelDelegate
        {
            get { return m_PixelDelegate; }
            set
            {
                if (value == null) throw new ArgumentNullException("value");
                m_PixelDelegate = value;
            }
        }

        /// <summary>
        /// Execute this Filter
        /// A Debug Statement is written if a concrete Filter is called
        /// </summary>
        /// <param name="_bitmap"></param>
        /// <returns></returns>
        public override UnsafeBitmap Execute(UnsafeBitmap _bitmap)
        {
            Apply(_bitmap, m_PixelDelegate);
            return _bitmap;
        }

        /// <summary>
        /// Applies the specified _bitmap.
        /// </summary>
        /// <param name="_bitmap">The _bitmap.</param>
        /// <param name="_pixelDelegate">The _pixel delegate.</param>
        public virtual void Apply(UnsafeBitmap _bitmap, PixelDelegate _pixelDelegate)
        {
            if (_pixelDelegate == null)
            {
                throw new ArgumentNullException("pd");
            }

            PixelDelegate oldDelegate = m_PixelDelegate;

            try
            {
                m_PixelDelegate = _pixelDelegate;
                PerformActualCommand(_bitmap);
            }
            finally
            {
                m_PixelDelegate = oldDelegate;
            }
        }

        /// <summary>
        /// Performs the actual command.
        /// </summary>
        /// <param name="source">The source.</param>
        /// <returns></returns>
        protected UnsafeBitmap PerformActualCommand(UnsafeBitmap source)
        {
            return PerformActualCommand(source, new Rectangle(0, 0, source.Width, source.Height));
        }

        /// <summary>
        /// Performs the actual command.
        /// </summary>
        /// <param name="_bitmap">The _bitmap.</param>
        /// <param name="imageArea">The image area.</param>
        /// <returns></returns>
        protected UnsafeBitmap PerformActualCommand(UnsafeBitmap _bitmap, Rectangle imageArea)
        {
            int nTop = imageArea.Top;
            int nHeight = imageArea.Height;
            int nLeft = imageArea.Left;
            int nWidth = imageArea.Width;
            Color sourceColor = Color.Transparent;
            Color destColor = Color.Transparent;
            for (int y = nTop; y < nHeight; y++)
            {
                for (int x = nLeft; x < nWidth; x++)
                {
                    sourceColor = _bitmap.GetPixel(x, y);
                    destColor = m_PixelDelegate(sourceColor, x, y);
                    _bitmap.SetPixel(x, y, destColor);
                }
            }
            return _bitmap;
        }

        /// <summary>
        /// This is used to clone a filter, each filter must implement it's own clone functionality
        /// </summary>
        /// <returns></returns>
        public override object Clone()
        {
            return new LambdaCommand(m_PixelDelegate);
        }
    }
}